package com.cake.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cake.base.util.ReturnUtils;
import com.cake.entity.XxProduct;
import com.cake.entity.pojo.ProductPoJo;
import com.cake.service.inter.IXxProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 商品控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "product")
public class ProductController {
	
	@Autowired
	private IXxProductService xxProductService;
	
	/**
	 * 查询所有的商品
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "selectProducts")
	public Object selectProducts(@RequestBody XxProduct xxProrduct) throws Exception{
		PageHelper.startPage(xxProrduct.getCurrentPage(), xxProrduct.getPageSize());
		List<XxProduct> list = xxProductService.selectProductInfos(xxProrduct);
		for (XxProduct xxProduct : list) {
			xxProduct.setTypeName(xxProduct.getIsCake() ? "新鲜" : "常温");
		}
		PageInfo<XxProduct> page = new PageInfo<>(list);
		return ReturnUtils.forNoramlPage("000000", list, 
				page.getPageNum(), page.getPageSize(), page.getPages(), (int)page.getTotal());
	}
	
	/**
	 *	根据id查询上商品详情
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectProductById")
	public Object selectProductById(Long productId) throws Exception{
		return ReturnUtils.forNoramlSuccess(xxProductService.selectProductDetails(productId));
	}
	
	/**
	 * 添加或者修改一条商品
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "insertOrUpdateProduct")
	public Object insertOrUpdateProduct(@RequestBody ProductPoJo productPoJo, HttpServletRequest request) throws Exception{
		xxProductService.insertOrUpdateProductAndSpecifications(productPoJo);
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
}
